package com.sex.bull.melancholy.service.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import utils.JsonUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by donghang on 16/6/13.
 */
@Component
public class RedisHashUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 判断hash中存在参数key
     * @param key
     * @param hashkey
     * @return
     */
    public boolean isNotEmpty(String key, String hashkey){
        return redisTemplate.opsForHash().size(key)>0 &&
                redisTemplate.opsForHash().hasKey(key ,hashkey);
    }
    /**
     * 判断hash中不存在参数key
     * @param key
     * @param hashkey
     * @return
     */
    public boolean isEmpty(String key, String hashkey){
        return !isNotEmpty(key, hashkey);
    }

    /**
     * 获取map值
     * @param key
     * @param hashkey
     * @return
     */
    public String get(String key, String hashkey){
        if(isNotEmpty(key, hashkey)){
            return (String)redisTemplate.opsForHash().get(key, hashkey);
        }
        return null;
    }

    /**
     * 获取map json值并转换成对象
     * @param key
     * @param hashkey
     * @param obj
     * @param <T>
     * @return
     */
    public <T> T get(String key, String hashkey, Class<T> obj){
        String hashStr = get(key, hashkey);
        if(StringUtils.isNoneBlank(hashkey)){
            return JsonUtils.json2Obj(hashStr, obj);
        }
        return null;
    }

    public Map getAll(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 字符串存入hash中
     * @param key
     * @param hashkey
     * @param dataStr
     * @return
     */
    public boolean put(String key, String hashkey, String dataStr){
        return put(key, hashkey, dataStr, -1);
    }
    /**
     * 字符串存入hash中并设置过期时间
     * @param key
     * @param hashkey
     * @param dataStr
     * @return
     */
    public boolean put(String key, String hashkey, String dataStr, int validTime){
        if(null != dataStr){
            redisTemplate.opsForHash().put(key, hashkey, dataStr);
            if(validTime != -1){
                redisTemplate.boundHashOps(key).expire(validTime, TimeUnit.MINUTES);
            }
            return true;
        }
        return false;
    }
    /**
     * json串存入哈希中
     * @param key
     * @param hashkey
     * @param dataObj 存入对象
     * @return
     */
    public <T> boolean put(String key, String hashkey, T dataObj){
        return put(key, hashkey, dataObj, -1);
    }

    /**
     * json串存入哈希中并设置过期时间
     * @param key
     * @param hashkey
     * @param dataObj
     * @param validTime 失效分钟
     * @param <T>
     * @return
     */
    public <T> boolean put(String key, String hashkey, T dataObj, int validTime){
        if(null != dataObj){
            return put(key, hashkey, JsonUtils.obj2Json(dataObj), validTime);
        }
        return false;
    }

    /**
     * 删除key的hash中一或多个hashkey的value
     * @param key
     * @param hashkeys 一或多个待删除的hashkey
     * @return
     */
    public boolean delete(String key, String... hashkeys){
        if(StringUtils.isNotBlank(key) && hashkeys.length>0) {
            redisTemplate.opsForHash().delete(key, hashkeys);
            return true;
        }
        return false;
    }

    /**
     * 删除key的整个hash
     * @param key
     * @return
     */
    public boolean delete(String key){
        if(StringUtils.isNotBlank(key)){
            redisTemplate.delete(key);
            return true;
        }
        return false;
    }

}
